package com.customization.unionpay.market.workflowaction;

import com.customization.unionpay.market.util.FormDataUtils;
import com.customization.unionpay.market.util.PropUtils;
import weaver.integration.logging.Logger;
import weaver.integration.logging.LoggerFactory;
import weaver.interfaces.workflow.action.Action;
import weaver.soa.workflow.request.RequestInfo;
import weaver.workflow.request.RequestManager;

import java.util.Map;

/**
 * @author sjm
 * @description 资金划拨单营销相关(归档)
 */
public class MarketOfFundTransferEndAction implements Action {

    private static final Logger log = LoggerFactory.getLogger(MarketOfFundTransferEndAction.class);

    @Override
    public String execute(RequestInfo requestInfo) {

        RequestManager rm = requestInfo.getRequestManager();

        try {
            MarketOfFundTransferAction marketOfFundTransferAction = new MarketOfFundTransferAction();
            // 流程表单id
            int formId = rm.getFormid();
            // 划拨类型
            String hblxYx = com.customization.unionpay.market.util.FormDataUtils.getItemValue(formId, "hblx", "营销资金补足");
            log.info("营销资金补足：" + hblxYx);
            Map<String, Object> allFormData = com.customization.unionpay.market.util.FormDataUtils.getAllFormData(requestInfo);
            Map<String, String> mainMap = (Map<String, String>) allFormData.get("mainMap");
            String hblx = mainMap.get("hblx");
            log.info("hblx：" + hblx);
            if(hblx.equals(hblxYx)){
                log.info("营销发起资金划拨单!");
                // 营销活动日报表台账表单id
                int reportFormId = Integer.parseInt(PropUtils.BB_LOG.getPropValue("marketSubject", "marketing_activity_daily_report_form_id"));
                String qksptgValue = com.customization.unionpay.market.util.FormDataUtils.getItemValue(reportFormId, "zt", "请款审批通过");
                log.info("请款审批通过枚举值：" + qksptgValue);
                String qkspwtgValue = com.customization.unionpay.market.util.FormDataUtils.getItemValue(reportFormId, "zt", "请款审批未通过");
                log.info("请款审批未通过枚举值：" + qkspwtgValue);
                String fkzValue = com.customization.unionpay.market.util.FormDataUtils.getItemValue(reportFormId, "zt", "付款中");
                log.info("付款中枚举值：" + fkzValue);
                String fzsbValue = com.customization.unionpay.market.util.FormDataUtils.getItemValue(reportFormId, "zt", "付款失败");
                log.info("付款失败枚举值：" + fzsbValue);
                String yfkValue = com.customization.unionpay.market.util.FormDataUtils.getItemValue(reportFormId, "zt", "已付款");
                log.info("已付款枚举值：" + yfkValue);
                String nbyxValue = com.customization.unionpay.market.util.FormDataUtils.getItemValue(reportFormId, "zjly", "内部营销");
                log.info("内部营销枚举值：" + nbyxValue);
                String nwbyxValue = com.customization.unionpay.market.util.FormDataUtils.getItemValue(reportFormId, "zjly", "内外部联合营销");
                log.info("内外部联合营销枚举值：" + nwbyxValue);

                String yzfValue = com.customization.unionpay.market.util.FormDataUtils.getItemValue(formId, "fkzt", "已支付");
                log.info("已支付枚举值：" + yfkValue);
                String dailyReportTableName = FormDataUtils.getTableName(reportFormId);
                log.info("dailyReportTableName："+dailyReportTableName);

                // 所有明细表的值
                Map<String, Map<String, Map<String, String>>> detailMap = (Map<String, Map<String, Map<String, String>>>) allFormData.get("detailMap");

                Map<String, Map<String, String>> dt1 = detailMap.get("dt1");

                marketOfFundTransferAction.updateDailyState(dt1,dailyReportTableName,3, qksptgValue, qkspwtgValue, fkzValue, fzsbValue, yfkValue, yzfValue,nbyxValue,nwbyxValue);
            }else {
                log.info("非营销发起资金划拨单!");
            }
        } catch (Exception e) {
            // 异常报错是填写异常信息，阻止流程继续流转
            requestInfo.getRequestManager().setMessageid("10001");
            requestInfo.getRequestManager().setMessagecontent(e.getMessage());
            return Action.FAILURE_AND_CONTINUE;
        }
        return Action.SUCCESS;
    }

}
